
#include "stdafx.h"


bool nlmtool_params_t::init( int argc, wchar_t* argv[ ] )
{
	cmd_line.argc = argc;
	cmd_line.argv = argv;


	help_requested = false;


	print_overall_connectivity = true;


	network.first = false;
	network.index = -1;

	network.guid_arg = nullptr;
	network.guid = { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };

	network.connectivity = NoConnectivity;
	network.connectivity_set = false;

	network.category = NLM_NETWORK_CATEGORY_PUBLIC;
	network.category_set = false;

	network.domain_type = NLM_DOMAIN_TYPE_NON_DOMAIN_NETWORK;
	network.domain_type_set = false;

	network.name;
	network.description;

	network.datetime_created = { 0, 0, 0, 0, 0, 0, 0, 0 };
	network.datetime_connected = { 0, 0, 0, 0, 0, 0, 0, 0 };


	network.new_category = NLM_NETWORK_CATEGORY_PUBLIC;
	network.new_category_set = false;

	network.new_name = nullptr;
	network.new_description = nullptr;


	for( int i = 0; i < argc; ++i )
		if( argv[ i ] && argv[ i ][ 0 ] == L'-' )
		{
			if( _wcsicmp( argv[ i ], L"-help" ) == 0 )
			{
				help_requested = true;

				return true;
			}

			if( _wcsicmp( argv[ i ], L"-first" ) == 0 )
			{
				network.first = true;

				continue;
			}

			if( _wcsicmp( argv[ i ], L"-index" ) == 0 )
			{
				if( ++i < argc )
					network.index = _wtoi( argv[ i ] );
				else
					fwprintf( stderr, L"cmdline: '-index' requires an integer value; will ignore this parameter.\n" );

				continue;
			}

			if( _wcsicmp( argv[ i ], L"-networkid" ) == 0 )
			{
				if( ++i < argc )
				{
					if( CLSIDFromString( argv[ i ], &network.guid ) == NOERROR )
						network.guid_arg = argv[ i ];
					else
						fwprintf( stderr, L"cmdline: '-networkid' got invalid value: %s; requires a GUID: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}; will ignore this parameter.\n", argv[ i ] );
				}
				else
					fwprintf( stderr, L"cmdline: '-networkid' requires a GUID: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}; will ignore this parameter.\n" );

				continue;
			}

			if( _wcsicmp( argv[ i ], L"-ipv4" ) == 0 )
			{
				if( ++i < argc )
				{
					if( _wcsicmp( argv[ i ], L"notraffic" ) == 0 )
						network.connectivity |= NLM_CONNECTIVITY_IPV4_NOTRAFFIC;
					else if( _wcsicmp( argv[ i ], L"subnet" ) == 0 )
						network.connectivity |= NLM_CONNECTIVITY_IPV4_SUBNET;
					else if( _wcsicmp( argv[ i ], L"localnet" ) == 0 )
						network.connectivity |= NLM_CONNECTIVITY_IPV4_LOCALNETWORK;
					else if( _wcsicmp( argv[ i ], L"internet" ) == 0 )
						network.connectivity |= NLM_CONNECTIVITY_IPV4_INTERNET;
					else if( _wcsicmp( argv[ i ], L"any" ) == 0 )
						network.connectivity |= IPv4Connectivity;
					else
						fwprintf( stderr, L"cmdline: '-ipv4' got invalid value: %s; valid values are: { NOTRAFFIC | SUBNET | LOCALNET | INTERNET | ANY }; will ignore this parameter.\n", argv[ i ] );
				}
				else
					fwprintf( stderr, L"cmdline: '-ipv4' requires a value: { NOTRAFFIC | SUBNET | LOCALNET | INTERNET | ANY }; will ignore this parameter.\n" );

				continue;
			}

			if( _wcsicmp( argv[ i ], L"-ipv6" ) == 0 )
			{
				if( ++i < argc )
				{
					if( _wcsicmp( argv[ i ], L"notraffic" ) == 0 )
						network.connectivity |= NLM_CONNECTIVITY_IPV6_NOTRAFFIC;
					else if( _wcsicmp( argv[ i ], L"subnet" ) == 0 )
						network.connectivity |= NLM_CONNECTIVITY_IPV6_SUBNET;
					else if( _wcsicmp( argv[ i ], L"localnet" ) == 0 )
						network.connectivity |= NLM_CONNECTIVITY_IPV6_LOCALNETWORK;
					else if( _wcsicmp( argv[ i ], L"internet" ) == 0 )
						network.connectivity |= NLM_CONNECTIVITY_IPV6_INTERNET;
					else if( _wcsicmp( argv[ i ], L"any" ) == 0 )
						network.connectivity |= IPv6Connectivity;
					else
						fwprintf( stderr, L"cmdline: '-ipv6' got invalid value: %s; valid values are: { NOTRAFFIC | SUBNET | LOCALNET | INTERNET | ANY }; will ignore this parameter.\n", argv[ i ] );
				}
				else
					fwprintf( stderr, L"cmdline: '-ipv6' requires a value: { NOTRAFFIC | SUBNET | LOCALNET | INTERNET | ANY }; will ignore this parameter.\n" );

				continue;
			}

			if( _wcsicmp( argv[ i ], L"-disconnected" ) == 0 )
			{
				network.connectivity |= ConnectivityDisconnected;

				continue;
			}

			if( _wcsicmp( argv[ i ], L"-category" ) == 0 )
			{
				if( ++i < argc )
				{
					if( _wcsicmp( argv[ i ], L"PUBLIC" ) == 0 )
					{
						network.category = NLM_NETWORK_CATEGORY_PUBLIC;
						network.category_set = true;
					}
					else if( _wcsicmp( argv[ i ], L"PRIVATE" ) == 0 )
					{
						network.category = NLM_NETWORK_CATEGORY_PRIVATE;
						network.category_set = true;
					}
					else if( _wcsicmp( argv[ i ], L"DOMAIN" ) == 0 )
					{
						network.category = NLM_NETWORK_CATEGORY_DOMAIN_AUTHENTICATED;
						network.category_set = true;
					}
					else
						fwprintf( stderr, L"cmdline: '-category' got invalid value: %s; valid values are: { PUBLIC | PRIVATE | DOMAIN }; will ignore this parameter.\n", argv[ i ] );
				}
				else
					fwprintf( stderr, L"cmdline: '-category' requires a value: { PUBLIC | PRIVATE | DOMAIN }; will ignore this parameter.\n" );

				continue;
			}

			if( _wcsicmp( argv[ i ], L"-domaintype" ) == 0 )
			{
				if( ++i < argc )
				{
					if( _wcsicmp( argv[ i ], L"NONDOMAIN" ) == 0 )
					{
						network.domain_type = NLM_DOMAIN_TYPE_NON_DOMAIN_NETWORK;
						network.domain_type_set = true;
					}
					else if( _wcsicmp( argv[ i ], L"DOMAIN" ) == 0 )
					{
						network.domain_type = NLM_DOMAIN_TYPE_DOMAIN_NETWORK;
						network.domain_type_set = true;
					}
					else if( _wcsicmp( argv[ i ], L"AUTHENTICATED" ) == 0 )
					{
						network.domain_type = NLM_DOMAIN_TYPE_DOMAIN_AUTHENTICATED;
						network.domain_type_set = true;
					}
					else
						fwprintf( stderr, L"cmdline: '-domaintype' got invalid value: %s; valid values are: { NONDOMAIN | DOMAIN | AUTHENTICATED }; will ignore this parameter.\n", argv[ i ] );
				}
				else
					fwprintf( stderr, L"cmdline: '-domaintype' requires a value: { NONDOMAIN | DOMAIN | AUTHENTICATED }; will ignore this parameter.\n" );

				continue;
			}

			if( _wcsicmp( argv[ i ], L"-name" ) == 0 )
			{
				if( ++i < argc )
					network.name = argv[ i ];
				else
					fwprintf( stderr, L"cmdline: '-name' requires a value; will ignore this parameter.\n" );

				continue;
			}

			if( _wcsicmp( argv[ i ], L"-description" ) == 0 )
			{
				if( ++i < argc )
					network.description = argv[ i ];
				else
					fwprintf( stderr, L"cmdline: '-description' requires a value; will ignore this parameter.\n" );

				continue;
			}


			if( _wcsicmp( argv[ i ], L"-setcategory" ) == 0 )
			{
				if( ++i < argc )
				{
					if( _wcsicmp( argv[ i ], L"PUBLIC" ) == 0 )
					{
						network.new_category = NLM_NETWORK_CATEGORY_PUBLIC;
						network.new_category_set = true;
					}
					else if( _wcsicmp( argv[ i ], L"PRIVATE" ) == 0 )
					{
						network.new_category = NLM_NETWORK_CATEGORY_PRIVATE;
						network.new_category_set = true;
					}
					else
						fwprintf( stderr, L"cmdline: '-setcategory' got invalid value: %s; valid values are: { PUBLIC | PRIVATE }; will ignore this parameter.\n", argv[ i ] );
				}
				else
					fwprintf( stderr, L"cmdline: '-setcategory' requires a value: { PUBLIC | PRIVATE }; will ignore this parameter.\n" );

				continue;
			}

			if( _wcsicmp( argv[ i ], L"-setname" ) == 0 )
			{
				if( ++i < argc )
					network.new_name = argv[ i ];
				else
					fwprintf( stderr, L"cmdline: '-setname' requires a value; will ignore this parameter.\n" );

				continue;
			}

			if( _wcsicmp( argv[ i ], L"-setdescription" ) == 0 )
			{
				if( ++i < argc )
					network.new_description = argv[ i ];
				else
					fwprintf( stderr, L"cmdline: '-setdescription' requires a value; will ignore this parameter.\n" );

				continue;
			}
		} // outer 'if'

	if( network.connectivity == NoConnectivity )
		network.connectivity = IPv4Connectivity | IPv6Connectivity;
	else if( network.connectivity != ConnectivityDisconnected )
	{
		if( ( network.connectivity & IPv4Connectivity ) == NoConnectivity )
			network.connectivity |= IPv4Connectivity; 

		if( ( network.connectivity & IPv6Connectivity ) == NoConnectivity )
			network.connectivity |= IPv6Connectivity; 
	}


	return true;
}
